#!/usr/bin/python

import subprocess
import re
import sys
import os

class PackageListGenerateTool:

    def gen_list(self, os_release, build, to_path='./', ips_url='file:///net/ipsbj.cn.oracle.com/dtrepo/'):
        command = "pkg info -g {0}/{1}/{2} '*'".format(ips_url, os_release, build)
        p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        if stderr:
            sys.stderr.write('Package List Generation Fails:\n' + stderr)
            return -1
        #since version is differrent for every build, diff can not find the changes of pkg flag
        full_list_file = 'pkg_{0}_b{1}_full.list'.format(os_release, build)
        full_list_handle = open(to_path+full_list_file, 'w')
        name_str = 'Name:'
        state_str = 'State:'
        category_str = 'Category:'
        version_str = 'Version:'
        o_str = 'Obsolete'
        r_str = 'Renamed'
        category_cut_str = '\(org.opensolaris.category.2008\)'
        for p in stdout.split('\n\n'):
            pkg_block_lines = p.split('\n')
            flag = '?'
            for i,l in enumerate(pkg_block_lines):
                if version_str in l or name_str in l:
                    full_list_handle.write(l.strip() + '****')
                elif category_str in l:
                    index = i
                    #since a package may have multiple category, use a loop to get the last one
                    while index < len(pkg_block_lines):
                        if ':' in pkg_block_lines[index+1]:
                            break
                        index += 1
                    to_print = re.sub(category_cut_str, '', pkg_block_lines[index]).strip() + '****'
                    if index > i:#when there are more than one category, 'Category:' should be added to the str
                        to_print = category_str + ' ' + to_print
                    full_list_handle.write(to_print)
                elif state_str in l:
                    if o_str in l:
                        flag = 'o' #obsolete
                    elif r_str in l:
                        flag = 'r' #renamed
                    elif not '(' in l:
                        flag = 'n' #normal
                    else:
                        sys.stderr.write('ERROR: abnormal flag found in {0} b{1}: {2}'.format(os_release, build, l))
            full_list_handle.write('Flag: {0}\n'.format(flag))
        full_list_handle.close()
        return 0

if __name__ == '__main__':
    cwd = os.path.dirname(__file__)
    PackageListGenerateTool().gen_list('s12', '04', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '05', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '06', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '07', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '08', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '09', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '10', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '11', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '12', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '13', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '14', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '15', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '16', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '17', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '18', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '19', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '20', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '21', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '22', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '23', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '24', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '25', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '26', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '27', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '28', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '29', to_path=cwd+'/pkg_list/')
    PackageListGenerateTool().gen_list('s12', '30', to_path=cwd+'/pkg_list/')
